package com.vercon.moensmart.dialog;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.SeekBar;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;

import com.vercon.common.manager.AudioMngHelper;
import com.vercon.moensmart.R;
import com.vercon.moensmart.databinding.DialogHomeVolumeLayoutBinding;
import com.vercon.moensmart.dialog.base.BaseDialogFragment;


public class HomeVolumeDialog extends BaseDialogFragment<DialogHomeVolumeLayoutBinding> {

    private OnDialogListener mDialogListener;
    private AudioMngHelper mHelper;//音量管理

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStyle(DialogFragment.STYLE_NORMAL, R.style.dialog_wifi_input);
        setCancelable(false);
    }

    protected DialogHomeVolumeLayoutBinding onCreateViewBinding(@NonNull LayoutInflater inflater, @Nullable ViewGroup parent) {
        return DialogHomeVolumeLayoutBinding.inflate(inflater,parent,false);
    }

    @Override
    protected void initView(View view) {
        mHelper = new AudioMngHelper(getContext());

        float a = (float) (mHelper.getCurrentVolume() / 1.5);
        if (a <= 1){
            mHelper.setVoice(0);
            getBinding().igVolumPic.setImageResource(R.mipmap.ic_home_volum_close);
        }else {
            getBinding().igVolumPic.setImageResource(R.mipmap.ic_home_volum_open);
        }
        getBinding().tvVolumeValue.setText((int)a + "%");

    }

    public void setOnDialogListener(OnDialogListener listener) {
        mDialogListener = listener;
    }

    public interface OnDialogListener {
        void onCancel();
        void onConfirm();
    }

    @Override
    protected void initListener(View view) {
        getBinding().igClose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dismiss();
                if(null != mDialogListener){
                    mDialogListener.onCancel();
                }
            }
        });

        //音量控制
        getBinding().volumeProgress.setMax(mHelper.getSystemMaxVolume());
        getBinding().volumeProgress.setProgress(mHelper.getCurrentVolume());
        getBinding().volumeProgress.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {

            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                mHelper.setVoice(progress);

                float a = (float) (progress / 1.5);
                if (a <= 1){
                    mHelper.setVoice(0);
                    getBinding().igVolumPic.setImageResource(R.mipmap.ic_home_volum_close);
                }else {
                    getBinding().igVolumPic.setImageResource(R.mipmap.ic_home_volum_open);
                }
                getBinding().tvVolumeValue.setText((int)a + "%");

            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });

    }



    @Override
    protected boolean isFullScreen() {
        return true;
    }

}